package com.company.indicationshots.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
//创建数据库类
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "laser_wave_simulate.db";
    private static final int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建表的SQL语句
        //用户表 user
        String CREATE_SHOOTER = "CREATE TABLE user (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "name TEXT NOT NULL," +                        //用户名
                "number TEXT NOT NULL," +                      //编号
                "isSelect INTEGER NOT NULL," +                 //是否已选,0否，1是，默认0
                "create_time TEXT NOT NULL)";                  //创建时间
        db.execSQL(CREATE_SHOOTER);

        //成绩表 score
        String CREATE_SCORE = "CREATE TABLE score (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "num INTEGER NOT NULL," +                     //发序
                "record_id INTEGER NOT NULL," +               //记录id
                "ring REAL NOT NULL," +                       //环数,0为脱靶
                "position TEXT," +                            //位置,空为脱靶
                "distance TEXT NOT NULL," +                   //距离
                "x REAL NOT NULL," +                          //靶横坐标,0为脱靶
                "y REAL NOT NULL," +                          //靶纵坐标,0为脱靶
                "time REAL NOT NULL," +                       //用时,0为脱靶
                "create_time TEXT NOT NULL)";                 //创建时间
        db.execSQL(CREATE_SCORE);

        //记录表 record
        String CREATE_RECORD = "CREATE TABLE record (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "shooter TEXT," +                             //射手
                "gun_type TEXT NOT NULL," +                   //枪型
                "sum INTEGER NOT NULL," +                     //总发数
                "miss INTEGER NOT NULL," +                    //脱靶
                "highest REAL NOT NULL," +                    //最高环数
                "avg REAL NOT NULL," +                        //平均环数
                "variance REAL NOT NULL," +                   //方差
                "create_time TEXT NOT NULL)";                 //创建时间
        db.execSQL(CREATE_RECORD);

        //通信设置表 commset
        String CREATE_COMMSET = "CREATE TABLE commset (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "targetType TEXT NOT NULL," +                 //靶型
                "wirelessModule TEXT NOT NULL," +             //无线模块
                "serialPort TEXT NOT NULL," +                 //串口名称
                "serialPortPath TEXT NOT NULL," +             //串口路径
                "baudRate INTEGER NOT NULL," +                //波特率
                "createTime TEXT NOT NULL)";                  //创建时间
        db.execSQL(CREATE_COMMSET);

        //功能设置表 funcset
        String CREATE_FUNCSET = "CREATE TABLE funcset (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "gunType TEXT NOT NULL," +                    //枪型
                "targetType TEXT NOT NULL," +                 //靶型
                "bulletCount INTEGER NOT NULL," +             //子弹数量
                "gunData INTEGER NOT NULL," +                 //枪数据，0关闭，1开启
                "shot INTEGER NOT NULL," +                    //枪声,0关闭，1开启
                "locationReport INTEGER NOT NULL," +          //位置播报，0关闭，1开启
                "shootDistance INTEGER NOT NULL," +           //射击距离
                "createTime TEXT NOT NULL)";                  //创建时间
        db.execSQL(CREATE_FUNCSET);

        //训练记录表 trainRecord
        String CREATE_TRAINRECORD = "CREATE TABLE trainRecord (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "shooter TEXT NOT NULL," +                    //射手
                "createTime TEXT NOT NULL)";                  //创建时间
        db.execSQL(CREATE_TRAINRECORD);

        //训练成绩表 trainScore
        String CREATE_TRAINSCORE = "CREATE TABLE trainScore (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "num INTEGER NOT NULL," +                     //序号
                "ring REAL NOT NULL," +                       //环数
                "time REAL NOT NULL," +                       //时间
                "trainRecordId INTEGER NOT NULL," +           //训练记录id
                "createTime TEXT NOT NULL)";                  //创建时间
        db.execSQL(CREATE_TRAINSCORE);

        //训练瞄准点表 aimPoint
        String CREATE_AIMPOINT = "CREATE TABLE aimPoint (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "x REAL NOT NULL," +                          //x值
                "y REAL NOT NULL," +                          //y值
                "trainScoreId INTEGER NOT NULL," +            //训练成绩id
                "createTime TEXT NOT NULL)";                  //创建时间
        db.execSQL(CREATE_AIMPOINT);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 数据库升级逻辑，如修改表结构等
    }
}
